﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPI.Client.Interface;
using PPI.Client.Utils;
using PPI.Entities.Enumerator;

namespace PPI.Client.Presenter
{
    public class AssociateUtilityPresenter
    {
        private IAssociateUtility m_view;
        private bool m_isSaved;


        public bool IsSaved
        {
            get { return m_isSaved; }
            set { m_isSaved = value; }
        }

        public AssociateUtilityPresenter(IAssociateUtility view)
        {
            this.m_view = view;
        }

        public void CreateNew()
        {

            try
            {
                switch (m_view.DatabaseType)
                {
                    case DatabaseTypes.GPS:
                        PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.AssociateWithVisit(m_view.IdToDuplicate, Int32.MinValue);
                        break;
                    case DatabaseTypes.Inspection:
                        PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.DuplicateByInspection(m_view.IdToDuplicate);
                        break;
                }
                m_isSaved = true;
                m_view.ShowSuccesfull();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
                m_view.ShowError();
                m_isSaved = false;
            }
            
        }

        public void Associate()
        {
            try
            {
                if(m_view.IdUtility > 0)
                {
                    switch (m_view.DatabaseType)
                    {
                        case DatabaseTypes.GPS:
                            PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.AssociateWithVisit(m_view.IdToDuplicate, m_view.IdUtility);
                            PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.AssociateWithVisitMobile(m_view.IdToDuplicate, m_view.IdUtility, m_view.ConnectionString);
                            break;
                        case DatabaseTypes.Inspection:
                            PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.AssociateWithInspection(m_view.IdToDuplicate, m_view.IdUtility);
                            PPI.Client.Logic.Utility.LUtility_Asset_XMissionStructure.AssociateWithInspectionMobile(m_view.IdToDuplicate, m_view.IdUtility, m_view.ConnectionString);
                            break;
                    }
                    m_isSaved = true;
                    m_view.ShowSuccesfull();
                }else
                {
                    m_view.ShowValidateMessage();
                    m_isSaved = false;
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
                m_view.ShowError();
                m_isSaved = false;
            }
        }
    }
}
